// =================================================================
// File: write.cpp
// Desc: Contains function for writing data in a file
// =================================================================

// =================================================================
// INCLUDES
// =================================================================

#include <string>
#include "FSManager.h"
#include "File.h"
#include "commands.h"
#include "config.h"

using namespace std;
// =================================================================
// FUNCTIONS
// =================================================================

// =================================================================
// Name: write
// Desc: Writes the requested data to the given fd
// Args: int fd - The file descriptor that is given by open.
//       string data - The data to be written in the file.
//       FSManager* fsm - pointer to active FSManager object
// Rets: -1 for failure to write, or the size of the data that was written
// =================================================================


int write(int fd, string data, FSManager *fsm){

  File *file = fsm->GetOpenFile(fd);

  if(file == NULL)
    return -1;

  if(file->GetMode() == WRITE_MODE || file->GetMode() == READWRITE_MODE){
    fsm->WriteFile(data,file);

    return data.length();
  }
  else{
    return -1;
  }


}
